//
//  CollectViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/31.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectNewsCell.h"
#import "CollectProgramaCell.h"
#import "DailyDetailViewController.h"
#import "NightMode.h"
#import "SearchDatabaseViewController.h"
#import "AppDelegate.h"

@interface CollectViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CollectProgramaCellDelegate>
@property (nonatomic,retain) UISegmentedControl * segment;
@property (nonatomic,retain) UICollectionView * collection;
@property (nonatomic,retain) CollectNewsCell * newsCell;
@property (nonatomic,retain) CollectProgramaCell * programaCell;
@end

@implementation CollectViewController
-(void)dealloc{
    [_programaCell release];
    [_segment release];
    [_collection release];
    [_newsCell release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    
    NSArray * segArr = @[@"资讯",@"栏目"];
    self.segment = [[UISegmentedControl alloc] initWithItems:segArr];
    _segment.selectedSegmentIndex = 0;
    _segment.frame = CGRectMake(0, 0, 140, 30);
    _segment.center = CGPointMake(self.view.frame.size.width/2, 34);
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    _segment.tintColor = [UIColor lightGrayColor];
    
    
    
    [self createCollection];
    [self createSearchButton];
    
    [self.view addSubview:_segment];
    [_segment release];
}
#pragma mark - 创建搜索按钮
-(void)createSearchButton{
    UIButton * searchButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 20, 30, 30)];
    searchButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"search_green" ofType:@"png"]]];
    [searchButton addTarget:self action:@selector(searchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:searchButton];
    [searchButton release];
    
    UIButton * childButton  =[[UIButton alloc] initWithFrame:CGRectMake(15, 20, 30, 30)];
    [self.view addSubview:childButton];
    [childButton setBackgroundImage:[UIImage imageNamed:@"child.png"] forState:UIControlStateNormal];
    [childButton addTarget:self action:@selector(childButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [childButton release];
    
    
    
}
- (void)childButtonClick
{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    YRSideViewController *sideViewController = [delegate sliderview];
    [sideViewController showLeftViewController:true];
}


-(void)searchButtonClick{
    SearchDatabaseViewController * searchDatabaseVC = [[SearchDatabaseViewController alloc] init];
    searchDatabaseVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // 切换界面动画效果
    [self presentViewController:searchDatabaseVC animated:YES completion:^{
        
    }];
    [searchDatabaseVC release];
}
#pragma mark - segment的点击
-(void)segmentAction:(UISegmentedControl *)seg{
    _collection.contentOffset = CGPointMake(seg.selectedSegmentIndex * [UIScreen mainScreen].bounds.size.width, 0);
}
-(void)createCollection{
    UICollectionViewFlowLayout * layout = [[[UICollectionViewFlowLayout alloc] init]autorelease];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 49);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 横屏滚动
    layout.minimumLineSpacing = 0; // 左右间隔为0
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor clearColor];
    _collection.showsHorizontalScrollIndicator = NO;
    _collection.bounces = NO;
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.pagingEnabled = YES;
    
    [_collection registerClass:[CollectNewsCell class] forCellWithReuseIdentifier:@"news"];
    [_collection registerClass:[CollectProgramaCell class] forCellWithReuseIdentifier:@"programa"];
    [self.view addSubview:_collection];
}
#pragma mark - collection的协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
        self.newsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"news" forIndexPath:indexPath];
        return _newsCell;
    }else{
        self.programaCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"programa" forIndexPath:indexPath];
        _programaCell.delegate = self;
        return _programaCell;
    }
}
#pragma mark - 滚动完成设置seg
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _segment.selectedSegmentIndex = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width;
}
// 视图将要出现，刷新两个cell，并根据是否是夜间模式设置UI
-(void)viewWillAppear:(BOOL)animated{
    [self.view setNeedsDisplay];
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    
    [_newsCell viewWillAppearReloadData];
    [_programaCell viewWillAppearReloadData];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return [NightMode nightModeON]?UIStatusBarStyleLightContent:UIStatusBarStyleDefault;
}
#pragma mark - CollectProgramaCellDelegate进入和退出详情
-(void)enterDetailWithModel:(ArticleModel *)model{
    DailyDetailViewController * detail = [[DailyDetailViewController alloc] initWithModel:model];
    [self presentViewController:detail animated:YES completion:^{
        
    }];
}
-(void)quitDetail{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
@end
